package com.ruicar.afs.cloud.seats.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.common.core.query.QueryCondition;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.seats.condition.FLowParamCondition;
import com.ruicar.afs.cloud.seats.entity.GroupScene;
import com.ruicar.afs.cloud.seats.feign.GroupSceneInfoFeign;
import com.ruicar.afs.cloud.seats.feign.UserDetailsInfoFeign;
import com.ruicar.afs.cloud.seats.service.GroupSceneService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author tiankai
 * @Describe 操作组场景
 * Created on 2020-07-25
 */
@Slf4j
@RestController
@RequestMapping("/scene")
@AllArgsConstructor
@Api(description = "操作组管理接口")
public class GroupSceneController {
    private GroupSceneInfoFeign feign;
    private GroupSceneService service;
    private UserDetailsInfoFeign detailsInfoFeign;

    @GetMapping(value = {"/getGroupSceneInfo"})
    @ApiOperation(value = "获取操作场景信息（分页）")
    public IResponse<Page<FLowParamCondition>> getGroupSceneInfo(@ModelAttribute FLowParamCondition condition) {
        QueryCondition<FLowParamCondition> fLowParamCondition = new QueryCondition();
        fLowParamCondition.setCondition(condition);
        fLowParamCondition.setPageNumber(condition.getPageNumber());
        fLowParamCondition.setPageSize(condition.getPageSize());
        IResponse<Page<FLowParamCondition>> listIResponse = feign.listFLowMapping(fLowParamCondition);
        List<FLowParamCondition> list = listIResponse.getData().getRecords();
        for (FLowParamCondition paramCondition : list) {
            GroupScene groupScene = service.getOne(Wrappers.<GroupScene>query().lambda().eq(GroupScene::getProcessId, paramCondition.getGreatWallServiceId()));
            if (ObjectUtil.isNotNull(groupScene)) {
                paramCondition.setDeptId(groupScene.getDeptId());
                paramCondition.setDeptName(detailsInfoFeign.getDeptInfo(groupScene.getDeptId()).getData().getTitle());
            }
        }
        return IResponse.success(listIResponse);
    }

    @RequestMapping(value = "/updateGroupScene", method = RequestMethod.POST)
    @ApiOperation(value = "更新操作组场景配置")
    public IResponse<GroupScene> updateGroupScene(@RequestBody FLowParamCondition condition) {
        GroupScene groupScene = service.getOne(Wrappers.<GroupScene>query().lambda().eq(GroupScene::getProcessId, condition.getGreatWallServiceId()));
        if (ObjectUtil.isNull(groupScene)) {
            groupScene = new GroupScene();
            groupScene.setDeptId(condition.getDeptId());
            groupScene.setProcessId(condition.getGreatWallServiceId());
        }
        groupScene.setDeptId(condition.getDeptId());
        service.saveOrUpdate(groupScene);
        return new IResponse().setData(groupScene);
    }
}
